package com.project.weimi.adapter;

import java.util.List;

import com.project.weimi.R;
import com.project.weimi.activity.MyCollectionActivity;
import com.project.weimi.entity.response.CollectionShopEntity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class CollectionGoodsAdapter extends BaseAdapter {

	private Context context;
	private List<CollectionShopEntity> list;

	public CollectionGoodsAdapter(Context context, List<CollectionShopEntity> list) {
		super();
		this.context = context;
		this.list = list;
	}

	public List<CollectionShopEntity> getList() {
		return list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder vh = new ViewHolder();
		// if(convertView == null){

		// 解析布局文件
		convertView = LayoutInflater.from(context).inflate(R.layout.collection_goods_listview_item, null);
		// 获得控件对象
		vh.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
		vh.img = (ImageView) convertView.findViewById(R.id.img);
		vh.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
		vh.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
		convertView.setTag(vh);
		// }else{
		// vh = (ViewHolder) convertView.getTag();
		// }

		// vh.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
		// {
		// @Override
		// public void onCheckedChanged(CompoundButton buttonView, boolean
		// isChecked) {
		//// if(isChecked){
		// list.get(position).setCheckBox(isChecked);
		//
		// if(context instanceof MyCollectionActivity){
		// ((MyCollectionActivity)context).getGoodsFrag().isAllCheck();
		// }
		//
		//// }else{
		//// list.get(position).setCheckBox(isChecked);
		////
		//// }
		// }
		// });

		vh.checkbox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				list.get(position).setCheckBox(vh.checkbox.isChecked());

				if (context instanceof MyCollectionActivity) {
					((MyCollectionActivity) context).getGoodsFrag().isAllCheck();
				}
			}
		});

		vh.checkbox.setChecked(list.get(position).isCheckBox());
		vh.img.setImageResource(list.get(position).getImg());
		vh.tv_content.setText(list.get(position).getContent());
		vh.tv_price.setText(list.get(position).getPrice() + "¥");

		return convertView;
	}

	class ViewHolder {
		CheckBox checkbox;
		ImageView img;
		TextView tv_content;
		TextView tv_price;
	}

}
